package com.zhilianbao.leyaogo.ui.adapter.me;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bql.recyclerview.swipe.SwipeMenuAdapter;
import com.bql.utils.DateUtils;
import com.zhilianbao.leyaogo.R;
import com.zhilianbao.leyaogo.model.response.me.MyCardVoucherResponse;
import com.zhilianbao.leyaogo.view.widgets.MoneyTextView;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by liu on 2016/11/3.
 */
public class CardVoucherAdapter extends SwipeMenuAdapter<CardVoucherAdapter.CardVoucherViewHolder> {
    private List<MyCardVoucherResponse> mList;
    private Context                     mContext;

    public CardVoucherAdapter(Context context, List<MyCardVoucherResponse> list) {
        mList = list;
        this.mContext = context;
    }

    @Override
    public View onCreateContentView(ViewGroup parent, int viewType) {
        return LayoutInflater.from(parent.getContext()).inflate(R.layout.item_card_voucher, parent, false);
    }

    @Override
    public CardVoucherViewHolder onCompatCreateViewHolder(View realContentView, int viewType) {
        return new CardVoucherViewHolder(realContentView);
    }

    @Override
    public void onBindViewHolder(CardVoucherViewHolder holder, int position) {
        holder.setData(mContext, mList.get(position));
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }


    static class CardVoucherViewHolder extends RecyclerView.ViewHolder {

        private       RelativeLayout mItemCardVoucher;
        private       TextView       mTvCardNum;
        private       MoneyTextView  mCardMoneyNum;
        private       TextView       mCardMaxNum;
        private       TextView       mCardSerialNumber;
        private       TextView       mCartPayLimit;
        private       ImageView      mIvHasUsed;
        private final TextView       mCardMoneyTitle;

        public CardVoucherViewHolder(View itemView) {
            super(itemView);
            mItemCardVoucher = (RelativeLayout) itemView.findViewById(R.id.item_card_voucher);
            mTvCardNum = (TextView) itemView.findViewById(R.id.tv_card_num);
            mCardMoneyNum = (MoneyTextView) itemView.findViewById(R.id.card_money_num);
            mCardMaxNum = (TextView) itemView.findViewById(R.id.card_max_num);
            mCardSerialNumber = (TextView) itemView.findViewById(R.id.card_serial_number);
            mCartPayLimit = (TextView) itemView.findViewById(R.id.card_pay_limit);
            mIvHasUsed = (ImageView) itemView.findViewById(R.id.iv_has_used);
            mCardMoneyTitle = (TextView) itemView.findViewById(R.id.card_money_title);
        }

        public void setData(Context context, MyCardVoucherResponse myCardVoucherResponse) {
            long endDay = myCardVoucherResponse.getEndDay();
            long beginDay = myCardVoucherResponse.getBeginDay();
            //卡类型 0 实物卡 1 网销卡 ,
            int cardType = myCardVoucherResponse.getCardItemType();
            String cardInfo = myCardVoucherResponse.getCardPwd();
            int payLimit = myCardVoucherResponse.getOrderPayLimit();

            String dateBeginDay = DateUtils.format(beginDay, "yyyy.MM.dd");
            String dateEndDay = DateUtils.format(endDay, "yyyy.MM.dd");
            long currentTimeMillis = System.currentTimeMillis();
            String dateToday = DateUtils.format(currentTimeMillis, "yyyy.MM.dd");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
            double cardBalance = myCardVoucherResponse.getCardBalance();
            double cardPrice = myCardVoucherResponse.getCardPrice();
            //金额很大时,会自动转换成科学计算法,用BigDecimal将其转化成String
            //            BigDecimal bdBalance = new BigDecimal(cardBalance);
            //            BigDecimal bigDecimalBalance = bdBalance.setScale(2, RoundingMode.HALF_UP);
            BigDecimal bdPrice = new BigDecimal(cardPrice);
            BigDecimal bigDecimalPrice = bdPrice.setScale(2, RoundingMode.HALF_UP);
            String cardNo = myCardVoucherResponse.getCardNo();
            mTvCardNum.setText("有效期: " + dateBeginDay + "-" + dateEndDay);
            mCardMaxNum.setText("总额: " + bigDecimalPrice + "元");
            mCardMoneyNum.setAmount(cardBalance);
            mCardSerialNumber.setText(cardType == 0 ? "卡号: " + cardNo : cardInfo);

            mCartPayLimit.setVisibility(cardType == 0 ? View.GONE : View.VISIBLE);
            mCartPayLimit.setText(cardType == 0 ? "" : String.format(context.getString(R.string.card_pay_limit), payLimit));
            // 实体卡为绿色，网销卡为蓝色
            mItemCardVoucher.setBackground(cardType == 0 ? context.getResources().getDrawable(R.drawable.bg_card_green) : context.getResources().getDrawable(R.drawable.bg_card_blue));

            try {
                Date nowBegin = sdf.parse(dateToday);
                Date nowEnd = sdf.parse(dateEndDay);

                if (cardBalance == 0) {//已使用
                    mItemCardVoucher.setBackground(context.getResources().getDrawable(R.drawable.bg_card_white));
                    mIvHasUsed.setVisibility(View.VISIBLE);
                    mIvHasUsed.setImageResource(R.drawable.label_already_used);
                    setCardTextColor(context.getResources().getColor(R.color.white));
                } else if (nowBegin.getTime() > nowEnd.getTime()) {//已过期
                    mItemCardVoucher.setBackground(context.getResources().getDrawable(R.drawable.bg_card_white));
                    mIvHasUsed.setVisibility(View.VISIBLE);
                    mIvHasUsed.setImageResource(R.drawable.label_expired);
                    setCardTextColor(context.getResources().getColor(R.color.color_999999));
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
            mIvHasUsed.setAlpha(70);
        }

        private void setCardTextColor(int color) {
            mCardMoneyNum.setBaseColor(color);
            mCardMoneyNum.setSymbolColor(color);
            mCardMoneyNum.setDecimalsColor(color);
            mTvCardNum.setTextColor(color);
            mCardMaxNum.setTextColor(color);
            mCardMoneyTitle.setTextColor(color);
            mCardSerialNumber.setTextColor(color);
        }
    }
}